package com.keba.lamp.complaint.controller;

import com.keba.basic.annotation.log.WebLog;
import com.keba.basic.base.R;
import com.keba.basic.exception.BizException;
import com.keba.lamp.complaint.strategy.CaseFileStrategy;
import com.keba.lamp.complaint.strategy.domain.CaseDownloadParam;
import com.keba.lamp.complaint.strategy.domain.CaseDownloadResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;

@Slf4j
@RequiredArgsConstructor
@Validated
@RestController
@RequestMapping("/complaintFile")
@Tag(name = "起诉状文件管理")
public class ComplaintFileController {

    @Resource
    private Map<String, CaseFileStrategy> caseFileStrategyMap;
    @Operation(summary = "下载文件")
    @PostMapping(value = "/downCaseFile")
    @WebLog(value = "下载文件", request = false)
    public R<String> downCaseFile(@RequestBody @Validated CaseDownloadParam param) {
        // 设置文件下载Header
        try {
            CaseFileStrategy caseFileStrategy = caseFileStrategyMap.get(param.getStrategy());
            if (caseFileStrategy == null) {
                return R.fail("文件下载类型错误");
            }
            CaseDownloadResult result = caseFileStrategy.exec(param);
            return StringUtils.isNotBlank(result.getUrl()) ? R.success(result.getUrl()) : R.fail("文件下载失败");
        } catch (Exception e) {
            log.error("下载失败:", e);
            throw new BizException("下载失败");
        }
    }


}
